package com.jackrain.nea.omslocal.Dao;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.util.TypeUtils;
import com.jackrain.nea.core.db.SPResult;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.util.Tools;
import com.jackrain.nea.util.ValueHolder;
import com.jackrain.nea.web.query.QueryEngine;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;

/**
 * @Discription: 确认奇门发货单创建dao
 * Created with IntelliJ IDEA.
 * @Author cs
 * @Date 2018/3/20 10:56
 */
@Component
@Slf4j
public class QiMenConfirmDeliveryorderDao {

    /**
     * 批量确认奇门发货单创建,消费到消息之后,调用存储过程
     */
    public void qiMenConfirmDeliveryOrder(JSONArray jsonArray, String msgKey, String topic) {

        QiMenConfirmDeliveryorderDao qmcd = ApplicationContextHandle.getBean(QiMenConfirmDeliveryorderDao.class);

        for (Object obj : jsonArray) {

            // 循环遍历 arr, 调用存储过程, 插入日志
            JSONObject tempObj = JSONObject.parseObject(obj.toString(), Feature.OrderedField);
            // 订单号  对应wing订单id
            int p_orderid = tempObj.getInteger("p_orderid");
            // 物流单号
            String p_logisticsno = tempObj.getString("p_logisticsno");
            // 物流公司编码
            String p_logisticscode = tempObj.getString("p_logisticscode");
            qmcd.invokingSP(p_orderid, p_logisticsno, p_logisticscode);
            int code = 0;
            String msg = "success";

            confirmDeliveryOrderLog(p_orderid, p_logisticsno, p_logisticscode, msg, code);
        }

    }

    /**
     * 单个确认奇门发货单创建,消费到消息之后,调用存储过程
     */
    public void qiMenConfirmDeliveryOrder(JSONObject jsonObject, String msgKey, String topic) {
        QiMenConfirmDeliveryorderDao qmcd = ApplicationContextHandle.getBean(QiMenConfirmDeliveryorderDao.class);

        int p_orderid = jsonObject.getInteger("p_orderid"); // 订单号  对应wing订单id
        String p_logisticsno = jsonObject.getString("p_logisticsno"); // 物流单号
        String p_logisticscode = jsonObject.getString("p_logisticscode"); // 物流公司编码
        qmcd.invokingSP(p_orderid, p_logisticsno, p_logisticscode);
        int code = 0;
        String msg = "success";
        confirmDeliveryOrderLog(p_orderid, p_logisticsno, p_logisticscode, msg, code);

    }

    /**
     * 调用存储过程，接收创建发货单确认数据
     *
     * @param p_orderid
     * @param p_logisticsno
     * @param p_logisticscode
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void invokingSP(int p_orderid, String p_logisticsno, String p_logisticscode) {
        ValueHolder vh = new ValueHolder();
        String spName = "sp_oms_drporderreturn";
        try {

            Collection collection = new ArrayList<Object>();
            collection.add(p_orderid);
            collection.add(p_logisticsno);
            collection.add(p_logisticscode);
            collection.add(0);
            collection.add("");
            collection.add("qimencloud");
            SPResult spResult = QueryEngine.getInstance().executeStoredProcedure(spName, collection, false);

        } catch (Exception e) {
            // 调用存储过程报错，将报错信息插入日志表
            vh.put("msg", "调用存储过程异常");
            vh.put("code", 99);
        }

    }

    /**
     * 日志记录
     *
     * @param p_orderid
     * @param p_logisticsno
     * @param p_logisticscode
     * @param msg
     */
    public void confirmDeliveryOrderLog(int p_orderid, String p_logisticsno, String p_logisticscode, String msg, int code) {

        String logInsertSql = "insert into t_omsdrpoutstoragelog (id,p_orderid,p_logisticsno,p_logisticscode,msg,code) values (?,?,?,?,?,?)";
        String logUpdateSql = "update t_omsdrpoutstoragelog set p_orderid = ? , p_logisticsno = ? ,p_logisticscode = ?,msg = ? , code = ?, modifieddate = ?";
        String querySql = "select id from t_omsdrpoutstoragelog where OMSERPORDERID = ? ";

        try {
            int id = Tools.getInt(QueryEngine.getInstance().doQueryOne(querySql, new Object[]{p_orderid}), -1);
            if (id < 1) {
                // 新增日志表
                String squnceSql = "select get_wsequences('t_omsdrpoutstoragelog') as SEQ from dual";
                int sid = TypeUtils.castToInt(QueryEngine.getInstance().doQueryOne(squnceSql));
                int result = QueryEngine.getInstance().executeUpdate(logInsertSql, new Object[]{sid, p_orderid, p_logisticsno, p_logisticscode, msg, code});

            } else {
                // 修改日志表
                Date modifieddate = new Date(System.currentTimeMillis());
                QueryEngine.getInstance().executeUpdate(logUpdateSql, new Object[]{p_orderid, p_logisticsno, p_logisticscode, msg, code, modifieddate});
            }

        } catch (Exception e) {
            // 插入日志报错，打印

        }

    }

}
